﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace TBG.TBG_F
{
    enum AIStates
    {
        Idle,
        Move,
        Attack,
        Defend,
        Waiting //in this state after each turn, waiting for enemy turn to finish
    }

    class AI
    {
        #region Attributes
        public SpriteFont _Font;
        public Texture2D _SpriteSheet;
        public Point _Position; //By _Position we can get Tile
        public int _Width = 48;
        public int _Height = 48;

        public bool _Alive = false;
        public playerStates _State;

        int _MAX_HP;
        public int _CURRENT_HP;
        int _ATTACK;
        int _DEFENCE;

        float actionRate = 2f;
        float fElapsed = .0f;
        #endregion

        #region Constructor
        public AI(ContentManager content)
        {
            _Position = new Point(15, 0);
            _Alive = true;
            _State = playerStates.Idle;

            _MAX_HP = 10;
            _CURRENT_HP = 10;
            _ATTACK = 2;
            _DEFENCE = 0;
            LoadContent(content);
        }
        #endregion

        #region loadContent, Update, Draw
        public void LoadContent(ContentManager content)
        {
            _SpriteSheet = content.Load<Texture2D>("characters");
            _Font = content.Load<SpriteFont>("Pericles8");
        }

        public void Update(GameTime gameTime, Player player)
        {
            fElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (fElapsed >= actionRate)
            {
                MoveToAttackY(player);
                fElapsed = 0f;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_SpriteSheet, getPosition(), getSourceRect(), Color.White); //new Vector2(735,49)
            spriteBatch.DrawString(_Font, getHPString(), new Vector2(getPosition().X, getPosition().Y - 10), Color.Black);
        }
        #endregion

        #region helper methods
        public Rectangle getSourceRect()
        {
                return new Rectangle(0, 0, _Width, _Height);
        }

        public Rectangle getPosition()
        {
            return new Rectangle(_Position.X *(1+ _Width), _Position.Y *(1+ _Height), _Width, _Height);
        }

        public void Attack()
        {
            //calculateHP();
        }

        public void Defend()
        { }

        public void Move(Point newPosition)
        {
            _Position = newPosition;
        }

        //BUILDING AI
        //No need to move to attack X since they will always be in there
        public void MoveToAttackY(Player player)
        {
            if (_Position.Y == player._Position.Y)
                return;
            if (_Position.Y > player._Position.Y)
                _Position.Y--;
            if (_Position.Y < player._Position.Y)
                _Position.Y++;
        }

        public void calculateHP(int Damage)
        {
            if (Damage > _DEFENCE)
            {
                _CURRENT_HP = _CURRENT_HP - (Damage - _DEFENCE);
            }
        }

        public string getHPString()
        {
            string info = string.Format("{0} / {1}",_CURRENT_HP, _MAX_HP);
            return info;
        }
        #endregion
    }
}
